//
//  XDChildMgrViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/4.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDChildMgrViewController.h"
#import "XDCitySelectViewController.h"
#import "HNRelativeModel.h"
#import "HNRelativeCell.h"

@interface XDChildMgrViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *childNameTextField;
//省份
@property (weak, nonatomic) IBOutlet UIView *provinceView;

@property (weak, nonatomic) IBOutlet UILabel *provieceLabel;

//市区
@property (weak, nonatomic) IBOutlet UIView *cityView;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;

//区域
@property (weak, nonatomic) IBOutlet UIView *areaView;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;

//选择年级
@property (weak, nonatomic) IBOutlet UIView *gradeView;
@property (weak, nonatomic) IBOutlet UILabel *gradeLabel;

//tableView
@property (weak, nonatomic) IBOutlet UITableView *tableView;


/** 选择的省份id */
@property(nonatomic,strong) NSNumber *selectedProvinceId;

/** 选择的城市id */
@property(nonatomic,strong) NSNumber *selectedCityId;

/** 选择的区域id */
@property(nonatomic,strong) NSNumber *selectedAreaId;

/** 选择的年级id */
@property(nonatomic,strong) NSNumber *selectedGradeId;


@end

@implementation XDChildMgrViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.view.frame = CGRectMake(0, 44, ScreenW, ScreenH - 44 - 64);
    
    self.childNameTextField.delegate = self;
    
    //添加手势
    [self addGesture];
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    
    [self.tableView registerNib:[UINib nibWithNibName:[HNRelativeCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
}


//添加点击手势
-(void)addGesture{

    UITapGestureRecognizer *proviceTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(proviceViewTap)];
    [_provinceView addGestureRecognizer:proviceTap];
    
    
    UITapGestureRecognizer *cityTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cityViewTap)];
    [_cityView addGestureRecognizer:cityTap];
    
    
    UITapGestureRecognizer *areaTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(areaViewTap)];
    [_areaView addGestureRecognizer:areaTap];
    
    
    UITapGestureRecognizer *gradeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gradeViewTap)];
    [_gradeView addGestureRecognizer:gradeTap];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.DataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HNRelativeCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    HNRelativeModel *Model = self.DataArr[indexPath.row];
    Cell.Model = Model;
    return Cell;
}

//省份点击
-(void)proviceViewTap{
    
    [self returnKey];
    
    XDCitySelectViewController *vc = [[XDCitySelectViewController alloc]init];
    WeakSelf(weakself)
    vc.selectedBlock = ^(NSNumber *selectedId , NSString *selectedName){
    
        weakself.provieceLabel.text = selectedName;
        weakself.selectedProvinceId = selectedId;
        NSLog(@"%@ -- %@",selectedId,selectedName);
        
        
        weakself.selectedCityId = nil;
        weakself.selectedAreaId = nil;
        weakself.cityLabel.text = @"";
        weakself.areaLabel.text = @"";
        
    };
    
    vc.selectType = SelectTypeProvince;
    [self.navigationController pushViewController:vc animated:YES];
}

//市点击
-(void)cityViewTap{
    
    [self returnKey];
    if (self.selectedProvinceId == nil) {
        [MBProgressHUD showError:@"请先选择省份"];
        return;
    }
    
    
    XDCitySelectViewController *vc = [[XDCitySelectViewController alloc]init];
    WeakSelf(weakself)
    //选择城市block
    vc.selectedBlock = ^(NSNumber *selectedId , NSString *selectedName){
        weakself.cityLabel.text = selectedName;
        weakself.selectedCityId = selectedId;
        NSLog(@"%@ -- %@",selectedId,selectedName);
    };
    vc.provinceid = self.selectedProvinceId;
    vc.selectType = SelectTypeCity;
    [self.navigationController pushViewController:vc animated:YES];
}

//区域
-(void)areaViewTap{
    [self returnKey];
    
    if (self.selectedCityId == nil) {
        [MBProgressHUD showError:@"没有对应区域"];
        return;
    }
    
    XDCitySelectViewController *vc = [[XDCitySelectViewController alloc]init];
    WeakSelf(weakself)
    //选择城市block
    vc.selectedBlock = ^(NSNumber *selectedId , NSString *selectedName){
        weakself.areaLabel.text = selectedName;
        weakself.selectedAreaId = selectedId;
        NSLog(@"%@ -- %@",selectedId,selectedName);
    };
    vc.cityid = self.selectedCityId;
    vc.selectType = SelectTypeArea;
    [self.navigationController pushViewController:vc animated:YES];
    
}

//年级点击
-(void)gradeViewTap{
    [self returnKey];
    
    
    XDCitySelectViewController *vc = [[XDCitySelectViewController alloc]init];
    WeakSelf(weakself)
    //选择城市block
    vc.selectedBlock = ^(NSNumber *selectedId , NSString *selectedName){
        weakself.gradeLabel.text = selectedName;
        weakself.selectedGradeId = selectedId;
        NSLog(@"%@ - %@",selectedId,selectedName);
    };
    vc.selectType = SelectTypeGrade;
    [self.navigationController pushViewController:vc animated:YES];
}

//添加btn
- (IBAction)addBtn:(id)sender {
    [self returnKey];
    
    [MBProgressHUD showHUD];
    NSDictionary *Dict = @{
                           @"regioninfo"    :[NSString stringWithFormat:@"%@-%@-%@",[self.selectedProvinceId stringValue],[self.selectedCityId stringValue],[self.selectedAreaId stringValue]],
                           @"nickname"      :self.childNameTextField.text,
                           @"phone"         :@"",
                           @"qid"           :[[[self.DataArr firstObject] qid] stringValue],
                           @"rolecode"      :@4,
                           @"gradeid"       :[self.selectedGradeId stringValue]
                           };
    [XDHttpTool post:BaseURL(@"qun.do?action=addquser") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Dict success:^(id responseObj) {
        
        if (responseObj)
        {
            NSMutableArray *Arr = [self.DataArr mutableCopy];
            [Arr addObject:[[HNRelativeModel alloc] init_HNWithObject:responseObj]];
            self.DataArr = [Arr copy];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
        else
        {
            [MBProgressHUD showError:responseObj[@"data"][@"msg"]];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:XDNetWorkError];
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self returnKey];
}

-(void)returnKey{
    
    [self.view endEditing:YES];
}



-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self returnKey];
    return YES;
}

@end
